import { createRouter, createWebHistory } from 'vue-router'
import { useAuthStore } from '@/stores/auth'

// 路由配置
const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue'),
    meta: { title: '登录', noAuth: true, hidden: true }
  },
  {
    path: '/',
    component: () => import('@/layout/Layout.vue'),
    redirect: '/cardProduct/cardProduct-list',
    meta: { requiresAuth: true },
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/Dashboard.vue'),
        meta: { 
          title: '仪表盘', 
          icon: 'Odometer',
          isParent: true,
          hidden: true
        }
      },
      {
        path: 'cardProduct',
        name: 'CardProduct',
        redirect: '/cardProduct/cardProduct-list',
        meta: { 
          title: '卡产品管理', 
          icon: 'Suitcase',
          isParent: true
        },
        children: [
          {
            path: 'cardProduct-list',
            name: 'ProductList',
            component: () => import('@/views/cardProduct/List.vue'),
            meta: { 
              title: '卡产品列表',
              isParent: false
            }
          },
        ]
      },
      {
        path: 'cardManage',
        name: 'CardManage',
        redirect: '/cardManage/card-list',
        meta: { 
          title: '卡片管理', 
          icon: 'CreditCard',
          isParent: true
        },
        children: [
          {
            path: 'card-list',
            name: 'CardtList',
            component: () => import('@/views/card/List.vue'),
            meta: { 
              title: '卡片列表',
              isParent: false
            }
          },
          {
            path: 'cardRecharge-list',
            name: 'CardRechargeList',
            component: () => import('@/views/cardRecharge/List.vue'),
            meta: { 
              title: '卡片充值列表',
              isParent: false
            }
          },
          {
            path: 'holderCard-list',
            name: 'HolderCardList',
            component: () => import('@/views/holderCard/List.vue'),
            meta: { 
              title: '持卡人列表',
              isParent: false
            }
          },
          {
            path: 'importCard',
            name: 'ImportCard',
            component: () => import('@/views/importCard/List.vue'),
            meta: { 
              title: '卡片入库数据',
              isParent: false
            }
          },
          {
            path: 'cardFee',
            name: 'CardFee',
            component: () => import('@/views/cardFee/List.vue'),
            meta: { 
              title: '卡费列表',
              isParent: false
            }
          },
          {
            path: 'cardTransfer',
            name: 'CardTransfer',
            component: () => import('@/views/cardTransfer/List.vue'),
            meta: { 
              title: '卡片转账列表',
              isParent: false
            }
          },
          {
            path: 'cardTransaction',
            name: 'CardTransaction',
            component: () => import('@/views/cardTransaction/List.vue'),
            meta: { 
              title: '卡交易列表',
              isParent: false
            }
          },
          {
            path: 'activation-code',
            name: 'ActivationCode',
            component: () => import('@/views/card/ActivationCode.vue'),
            meta: { 
              title: '实体卡激活码',
              isParent: false
            }
          },
        ]
      },
      {
        path: 'merchant',
        name: 'Merchant',
        redirect: '/merchant/list',
        meta: { 
          title: '商户管理', 
          icon: 'User',
          isParent: true
        },
        children: [
          {
            path: 'list',
            name: 'MerchantList',
            component: () => import('@/views/merchant/List.vue'),
            meta: { 
              title: '商户列表',
              isParent: false
            }
          },
          {
            path: 'upstreamPlatForm-list',
            name: 'UpstreamPlatForm',
            component: () => import('@/views/upstreamPlatForm/List.vue'),
            meta: { 
              title: '上游平台列表',
              isParent: false
            }
          },
          {
            path: 'merchantUpstreamConfig-list',
            name: 'MerchantUpstreamConfig',
            component: () => import('@/views/merchantUpstreamConfig/List.vue'),
            meta: { 
              title: '商户上游平台配置列表',
              isParent: false
            }
          },
          {
            path: 'webhookConfig-list',
            name: 'WebhookConfig',
            component: () => import('@/views/webhookConfig/List.vue'),
            meta: { 
              title: 'webhook配置列表',
              isParent: false
            }
          },
          {
            path: 'banking-av-info-list',
            name: 'BankingAvInfo',
            component: () => import('@/views/bankingAvInfo/List.vue'),
            meta: { 
              title: 'banking-av账户信息列表',
              isParent: false
            }
          },
          {
            path: 'banking-transfer-list',
            name: 'BankingTransfer',
            component: () => import('@/views/bankingTransfer/List.vue'),
            meta: { 
              title: 'banking转账列表',
              isParent: false
            }
          },
          {
            path: 'banking-receive-list',
            name: 'BankingReceiveList',
            component: () => import('@/views/bankingReceiveList/List.vue'),
            meta: { 
              title: 'banking互转接收账户列表',
              isParent: false
            }
          },
        ]
      },
      {
        path: 'manageUser',
        name: 'ManageUser',
        redirect: '/manageUser/manage-user-list',
        meta: { 
          title: '管理员管理', 
          icon: 'User',
          isParent: true
        },
        children: [
          {
            path: 'manage-user-list',
            name: 'ManageUserList',
            component: () => import('@/views/manageUser/List.vue'),
            meta: { 
              title: '管理员列表',
              isParent: false
            }
          },
        ]
      },
    ]
  },
  {
    path: '/404',
    name: 'NotFound',
    component: () => import('@/views/404.vue'),
    meta: { title: '页面不存在', noAuth: true, hidden: true }
  },
  {
    path: '/:pathMatch(.*)*',
    redirect: '/404',
    meta: { hidden: true }
  }
]

// 创建路由实例
const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const authStore = useAuthStore()
  
  // 设置页面标题
  if (to.meta.title) {
    document.title = `${to.meta.title} - 后台管理系统`
  }
  
  // 检查是否需要认证
  if (to.meta.requiresAuth) {
    // 如果用户未登录，跳转到登录页
    if (!authStore.token) {
      next('/login')
      return
    }
  }
  
  // 如果用户已登录且访问登录页，跳转到首页
  if (to.path === '/login' && authStore.token) {
    next('/')
    return
  }
  
  // 其他情况直接放行
  next()
})

// 路由后置守卫
router.afterEach((to, from) => {
  
})

export default router